/**********************************************************************************************/
/* FBL_UnicodeToFromNumeric.cpp                                           					  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2006															  */
/* All Rights Reserved.                                                 					  */
/**********************************************************************************************/

#include <VShared/FBL/publ/Headers/StdAfx.h>

#include <VShared/FBL/publ/Util_Strings/FBL_ustdio.h>
#include <VShared/FBL/publ/Util_Strings/FBL_UnicodeToFromNumeric.h>

#include <VShared/FBL/publ/Util_Strings/FBL_String_Len_Const.h>

#include <stdlib.h>
#include <stdio.h>


/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
//	UChar*  =>  long
//
long u_utol( const UChar* inStr )  
{
	char buf[kLongStrMaxLen + 1];
	if( inStr )
	{	
		ulong len = (ulong) vu_strlen(inStr);
		vu_u2a(inStr, buf, tslen(Min(len, kLongStrMaxLen) ) );
		
		return atol(buf);
	}
	else
	{
		return 0;
	}
}


/**********************************************************************************************/
//	long  =>  UChar*
//
UChar* u_ltou( long inNum, UChar* outBuf )
{
	int len;

	char buf[kLongStrMaxLen + 1];

	len = sprintf(buf, "%ld", inNum);
	vu_a2u(buf, outBuf, len );

	return outBuf + len;
}


/**********************************************************************************************/
//	UChar*  =>  ulong
//
ulong u_ustoul( const UChar* inStr )
{
	char buf[kULongStrMaxLen + 1];
	if( inStr )
	{
		ulong len = (ulong) vu_strlen(inStr);
		vu_u2a(inStr, buf, tslen( Min(len, kULongStrMaxLen) ) );

		char* stop_char = 0;    
		return strtoul(buf, &stop_char, 10);
	}
	else
	{
		return 0;
	}
}


/**********************************************************************************************/
//	ulong  =>  UChar*
//
UChar* u_ultous( ulong inNum, UChar* outBuf )
{
	char buf[kULongStrMaxLen + 1];

	int len = sprintf(buf, "%lu", inNum);
	vu_a2u(buf, outBuf, len );

	return outBuf + len;
}


/**********************************************************************************************/
//	UChar*  =>  double
//
double u_utof( const UChar* inStr )
{
	char buf[kDoubleStrMaxLen + 1];
	if( inStr )
	{
		ulong len = (ulong) vu_strlen(inStr);
		vu_u2a(inStr, buf, tslen( Min(len, kDoubleStrMaxLen) ) );

		return atof(buf);
	}
	else 
	{
		return 0.0;
	}
}


/**********************************************************************************************/
//	double  =>  UChar*
//
UChar* u_ftos( double inNum, UChar* outBuf )
{
	char buf[kDoubleStrMaxLen + 1];

	int len = sprintf( buf, "%g", inNum );	
	vu_a2u(buf, outBuf, len );

	return outBuf + len;
}


/**********************************************************************************************/
//	UChar*  =>  llong
//
llong u_utoll( const UChar* inStr )
{
	char buf[kLLongStrMaxLen + 1];
	if( inStr )
	{
		ulong len = (ulong) vu_strlen(inStr);
		vu_u2a(inStr, buf, tslen( Min(len, kLLongStrMaxLen) ) );
	}
	else
	{
		return 0;
	}

	llong value = 0;

#ifdef _MSC_VER
	sscanf(buf, "%I64i", &value);
#else
	sscanf(buf, "%lli", &value);
#endif //_MSC_VER

	return value;
}


/**********************************************************************************************/
//	llong  =>  UChar*
//
UChar* u_lltou( llong inNum, UChar* outBuf ) 
{
	char str[kLLongStrMaxLen + 1];
	
#ifdef 	_MSC_VER
	int len = sprintf( str, "%I64i", inNum );
#else
	int len = sprintf( str, "%lli", inNum );
#endif

	vu_a2u(str, outBuf, len );
	return outBuf + len;
}


/**********************************************************************************************/
//	UChar*  =>  ullong
//
ullong u_ustoull( const UChar* inStr )
{
	char buf[kULLongStrMaxLen + 1];
	
	if( inStr )
	{
		ulong len = (ulong) vu_strlen(inStr);
		vu_u2a(inStr, buf, tslen( Min(len, kULLongStrMaxLen)) );
	}
	else
		return 0;

	ullong value = 0;

#ifdef _MSC_VER
	sscanf(buf, "%I64u", &value);
#else
	sscanf(buf, "%llu", &value);
#endif //_MSC_VER

	return value;
}


/**********************************************************************************************/
//	ullong  =>  UChar*
//
UChar* u_ulltous( ullong inNum, UChar* outBuf )
{
	char str[kULLongStrMaxLen + 1];
	
#ifdef 	_MSC_VER
	int len = sprintf( str, "%I64u", inNum );
#else
	int len = sprintf( str, "%llu", inNum );
#endif

	vu_a2u(str, outBuf, len );
	return outBuf + len;
}


/**********************************************************************************************/
FBL_End_Namespace
